package com.ruoyi.pay.controller;

import com.ruoyi.pay.utils.QRCodeUtil;
import com.ruoyi.pay.utils.WxPayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/wxpayExam")
@CrossOrigin
public class WxpayExamController {

    @Autowired
    private WxPayUtils wxPayUtils;

    @GetMapping("/pay/{orderId}")
    public void pay(@PathVariable("orderId") String orderId, HttpServletResponse response) {
        //将订单信息发送给微信支付服务器，获取二维码的字符串
        String codeUrl = wxPayUtils.wxPay(orderId);
        System.out.println(codeUrl);
        //将二维码的字符串转成图片，返回给浏览器
        QRCodeUtil.createQRCode(response,codeUrl);
    }

    @GetMapping("/query/{orderId}")
    public Boolean pay(@PathVariable("orderId") String orderId) throws Exception {
        Boolean result = wxPayUtils.refreshWxPay(orderId);
        if(result == true){
            // TODO 根据订单编号修改订单状态
        }

        return result;
    }
}
